<?php
namespace app\Capital\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Model;
use think\Db;
use app\Capital\model\capital as CapitalModel;
class Invoice extends Admin{
    //发票
    //新增开票申请
    public function index(){
        $startDate=input('date1');
        $endDate=input('date2');
        $member=input('memebrid');
        $map=[];
        if($startDate && $endDate) {
            $map['create_time']=['between',[$startDate,$endDate]];
        }
        if($member){
            $me=explode('-',$member[0]);
            $memberid=$me[1];
            $map['member_id']=$memberid;
        }
        $data=[];
        if(isset($map['member_id'])){
            $data=Db::name('order')->where($map)->select();
        }
        $data=[
            ['order_id'=>1,'order_no'=>'jy4545','order_price'=>'680'],
            ['order_id'=>2,'order_no'=>'jy3615','order_price'=>'892.1'],
            ['order_id'=>3,'order_no'=>'jy5185','order_price'=>'7981.5'],
            ['order_id'=>4,'order_no'=>'jy9632','order_price'=>'1124']
        ];
        return ZBuilder::make('mix')
            ->addSelect('memberid','会员','','','','ajax','',['url'=>'/admin.php/Capital/adjustment/adjustment_list/type/1','datakey'=>'id','dataname'=>'name'])
            ->addDaterange('date1,date2', '日期范围','',"$startDate,$endDate")
            ->addBtn('<button type="submit" class="btn  btn-primary btn-rounded">确定搜索</button>')
            ->hideBtn('submit,back')
            ->layout(['memberid' => 2,'status'=>2, 'date1' => 4, 'date2' => 4, 'form_group_btnGroup' => 2])
            ->setPageTitle('待开票订单')// 设置页面标题
            ->setPrimaryKey('order_id')
            ->addColumns([ // 批量添加列
                ['order_id', 'ID'],
                ['order_no', '订单号'],
                ['order_price', '订单金额'],
                ['create_time', '创建时间']
            ])
            ->addTopButton('enable', ['title' => '申请开票','href'=>url('enable'),'class'=>'btn btn-success ajax-post'])
            ->addColumn('right_button', '操作', 'btn')
            ->addRightButton('edit', ['title' => '查看单据', 'icon' => 'fa fa-wrench'])// 批量添加右侧按钮
            ->setRowList($data)
            ->setPages()// 设置分页数据
            ->setFormMethod('get')
            ->fetch(); // 渲染页面
    }
    //单据开票
    public function enable($record = []){
        $ids   = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        if (empty($ids)) return $this->error('缺少主键');
        $where=[];
        $where['order_id']=['in',$ids];
        $data=Db::name('order')->where($where)->select();
        $data=[
            ['order_id'=>1,'order_no'=>'jy4545','order_price'=>'680','member_id'=>1],
            ['order_id'=>2,'order_no'=>'jy3615','order_price'=>'892.1'],
            ['order_id'=>3,'order_no'=>'jy5185','order_price'=>'7981.5'],
            ['order_id'=>4,'order_no'=>'jy9632','order_price'=>'1124']
        ];
        if($data){
            Db::startTrans();
            try{
               $order_list=Db::name("order_goods")->where($where)->select();
                if($order_list){
                }
                $re=Db::name('order')->where($where)->setField('is_invoice',1);
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                $this->error("申请发票失败");
            }
            $this->success("申请发票成功");
        }else{
            $this->error("未选择要开票的订单");
        }
    }
}